package com.example.pt.utils;

import com.example.pt.entity.User;
import com.example.pt.service.UserService;
import com.example.pt.task.UserAssessmentTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

@Component
public class DownloadUrlUtils {

    private final static Logger logger = LoggerFactory.getLogger(UserAssessmentTask .class);

    @Autowired
    private UserService userService;

    //头像下载链接
    public String generateDownloadLink(int uid) {

        if(userService.searchByUid(uid) != null){
            String headshot_path = userService.searchByUid(uid).getHeadshot();

            if(headshot_path == null){
                logger.info("headshot path is null");
                return null;
            }

            //替换反斜杠
            String filePath = headshot_path.replace("\\","/");

            String ipAddress;
            try{
                ipAddress = InetAddress.getLocalHost().getHostAddress();
            }catch (UnknownHostException e){
                logger.info("ip address is null");
                return null;
            }

            //构建下载链接
            String downloadUrl = "http://" + ipAddress + ":8080/download?filePath=" + URLEncoder.encode(filePath, StandardCharsets.UTF_8);
            return downloadUrl;
        }
        logger.info("uid查询user不存在！");
        return null;

    }

    //积分规则图片下载
    public String generateIntegralDownloadLink() {
        //替换反斜杠
        String filePath = "E:/PrivateTracker/picture/1.png";

        String ipAddress;
        try{
            ipAddress = InetAddress.getLocalHost().getHostAddress();
        }catch (UnknownHostException e){
            logger.info("ip address is null");
            return null;
        }

        //构建下载链接
        String downloadUrl = "http://" + ipAddress + ":8080/download?filePath=" + URLEncoder.encode(filePath, StandardCharsets.UTF_8);
        return downloadUrl;
    }


    /**
     * 生成种子文件下载链接
     * @param torrentPath 种子文件地址
     * @return 下载链接
     */
    public String generateTorrentDownloadLink(String torrentPath) {
        String ipAddress;
        try {
            ipAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            logger.error("获取服务器IP失败", e);
            return null;
        }

        try {
            // 编码文件路径
            if (torrentPath == null) {
                return "error";
            }

            // 替换路径中的反斜杠为正斜杠
            torrentPath = torrentPath.replace("\\", "/");
            
            // 获取原始文件名
            String fileName = torrentPath.substring(torrentPath.lastIndexOf("/") + 1);
            
            // 添加原始文件名作为参数，用于保持下载文件名
            return "http://" + ipAddress + ":8080/download/file?filePath=" + 
                   URLEncoder.encode(torrentPath, StandardCharsets.UTF_8) + 
                   "&fileName=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        } catch (Exception e) {
            logger.error("生成下载链接失败", e);
            return null;
        }
    }

}
